<template>
    <div>
    <el-form inline class="demo-form-inline">
    <el-row >
        <el-col :span="5">
            <el-form-item label="采购人Id">
                <el-input v-model="query.payload.assigneeId" placeholder="采购人Id" />
            </el-form-item>
        </el-col>
        <el-col :span="5">
            <el-form-item label="采购人姓名">
                <el-input v-model="query.payload.assigneeName" placeholder="采购人姓名" />
            </el-form-item>
        </el-col>
        <el-col :span="5">
            <el-form-item label="联系方式">
                <el-input v-model="query.payload.phone" placeholder="联系方式" />
            </el-form-item>
        </el-col>
        <el-col :span="5">
            <el-form-item label="优先级">
                <el-input v-model="query.payload.priority" placeholder="优先级" />
            </el-form-item>
        </el-col>
    </el-row>
    <el-row>
        <el-col :span="5">
            <el-form-item label="状态">
                <el-select v-model="query.payload.status" placeholder="请选择">
                    <el-option label="开启" :value = "1+''" />
                    <el-option label="关闭" :value = "0+''" />
                </el-select>
            </el-form-item>
        </el-col>
        <el-col :span="5">
            <el-form-item label="仓库Id">
                <el-input v-model="query.payload.wareId" placeholder="仓库Id" />
            </el-form-item>
        </el-col>
        <el-col :span="5">
            <el-form-item label="总金额">
                <el-input v-model="query.payload.amount" placeholder="总金额" />
            </el-form-item>
        </el-col>
    </el-row>
    <el-row >
        <el-form-item label="创建时间">
            <el-date-picker
                    v-model="query.dateRange"
                    type="datetimerange"
                    range-separator="到"
                    start-placeholder="开始时间"
                    end-placeholder="结束时间"
            />
        </el-form-item>
        <el-form-item>
            <el-button type="primary" activity @click="queryWmsPurchase">查询</el-button>
            <el-button type="warning" activity @click="reset">重置</el-button>
            <el-button type="success" @click="addWmsPurchase">新增</el-button>
        </el-form-item>
    </el-row>
    </el-form>
    <el-divider style="margin: 12px 0"/>
    <el-table ref="table" border v-loading="loading"  :data="data.tableData" header-cell-class-name="headerClass">
        <el-table-column align="center" type="index"  label="序号" width="55" />
        <!--<el-table-column align="center" prop="assigneeId" label="采购人Id" />-->
        <el-table-column align="center" prop="id"  label="采购单编号" width="130px"/>
        <el-table-column align="center" prop="assigneeName" label="采购人姓名" width="130px"/>
        <el-table-column align="center" prop="phone" label="联系方式" width="130px"/>
        <el-table-column align="center" prop="priority" label="优先级" width="100px"/>
        <el-table-column align="center" prop="status" label="状态" width="100px">
            <template #default="scope">
                <el-tag  effect="dark" v-if="scope.row.status===0">新建</el-tag>
                <el-tag type='info' effect="dark" v-else-if="scope.row.status===1">已分配</el-tag>
                <el-tag type='warning' effect="dark" v-else-if="scope.row.status===2">正在采购</el-tag>
                <el-tag type='success'  effect="dark" v-else-if="scope.row.status===3">已完成</el-tag>
                <el-tag type='danger'  effect="dark" v-else-if="scope.row.status===4">有异常</el-tag>
            </template>
        </el-table-column>
        <el-table-column align="center" prop="wareId" label="仓库Id" width="100px"/>
        <el-table-column align="center" prop="wareNameTemp" label="仓库名" width="150px"/>
        <el-table-column align="center" prop="amount" label="总金额" />
        <el-table-column align="center" prop="updateTime" label="更新时间" width="180px">
            <template #default="scope">
                {{moment(scope.row.updateTime).format("yyyy-MM-DD HH:mm:ss")}}
            </template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="200px">
            <template #default="scope">
                <div v-if="scope.row.status===0||scope.row.status===1">
                <el-link title="分配" type="primary" @click="distribute(scope.row.id)">
                    <el-icon :size="20">
                        <Edit />
                    </el-icon>
                    分配
                </el-link>
                &nbsp;
                <el-link title="修改" type="primary" @click="editWmsPurchase(scope.row.id)">
                    <el-icon :size="20">
                        <Edit />
                    </el-icon>
                    修改
                </el-link>
                &nbsp;
                <el-popconfirm v-show="scope.row.status==0" title="删除？" @confirm="deleteWmsPurchase(scope.row.id)"  @cancel="">
                    <template #reference>
                        <el-link type="danger" title="删除">
                            <el-icon :size="20">
                                <Delete />
                            </el-icon>
                            删除
                        </el-link>
                    </template>
                </el-popconfirm>
                </div>
            </template>
        </el-table-column>
</el-table>
<el-pagination
        style="display: flex;align-items:center;justify-content:center;"
        v-model:currentPage="query.currentPage"
        v-model:page-size="query.pageSize"
        :page-sizes="[10, 20, 30]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="query.total"
        @size-change="queryWmsPurchase"
        @current-change="queryWmsPurchase"
/>
<ModWmsPurchase @close="modWmsPurchase.isOpen = false"  @reload="queryWmsPurchase"
              ref="modWmsPurchase"
              :openType="transData.openType"
              :id="transData.id" />
<DistributeWmsPurchase @close="distributeWmsPurchase.isOpen = false"  @reload="queryWmsPurchase"
              ref="distributeWmsPurchase"
              :openType="transData2.openType"
              :id="transData2.id" />
</div>
</template>

<script setup>
    import ModWmsPurchase from '@/views/mall/wms/components/ModWmsPurchase.vue'
    import DistributeWmsPurchase from '@/views/mall/wms/components/DistributeWmsPurchase.vue'
    import request from '@/utils/request.js'
    import { ElMessage } from 'element-plus'
    import {ref, reactive, onMounted} from 'vue'
    import moment from 'moment'
    let transData = reactive({})
    let transData2 = reactive({})
    let modWmsPurchase = ref(null)
    let distributeWmsPurchase = ref(null)
    let data = reactive({
        modRole:false,
        tableData:[],
        openType:"",
        id:"",
    });
    let loading = ref(true)
    let query = reactive({dateRange:[],payload:{}, currentPage:1,pageSize:10,total:0})
    onMounted(() => {
        queryWmsPurchase()
    })
    function queryWmsPurchase(){
        query.startTime = query.dateRange[0]
        query.endTime = query.dateRange[1]
        request.post("/api/wmsPurchase/getWmsPurchasePage", query).then(resp => {
            data.tableData = resp.records
            query.total = resp.total
            loading.value = false
        })
    }

    function reset(){
        query.payload.assigneeId = ""
        query.payload.assigneeName = ""
        query.payload.phone = ""
        query.payload.priority = ""
        query.payload.status = ""
        query.payload.wareId = ""
        query.payload.amount = ""
        query.dateRange=[]
    }

    function addWmsPurchase(){
        transData.openType = "add"
        modWmsPurchase.value.isOpen = true
    }

    function deleteWmsPurchase(id){

        request.get(`/api/wmsPurchase/delete/${id}`).then(resp => {
            queryWmsPurchase()
            ElMessage({message: '删除成功！',type: 'success'})
        });
    }

    function editWmsPurchase(id){
        transData.id = id
        transData.openType = "edit"
        modWmsPurchase.value.isOpen = true
    }
    function distribute(id) {
        // 查看有无采购需求，没需求的无需分配
        request.get(`/api/wmsPurchase/checkHasDetail/${id}`).then(resp => {
            if(resp){
                transData2.id = id
                transData2.openType = "edit"
                distributeWmsPurchase.value.isOpen = true
            }else{
                ElMessage({message: '该采购单没有采购需求，无法分配！',type: 'error'})
            }
        });
    }
</script>
<style lang="css">

</style>


